package error

import (
	"errors"
	"fmt"
	"time"
)

type FileError struct {
	Op   string
	Path string
	Err  error
}

func (e *FileError) Error() string {
	return fmt.Sprintf("%s %s: %v", e.Op, e.Path, e.Err)
}

func ReadFile(path string) ([]byte, error) {
	return nil, &FileError{Op: "read", Path: path, Err: errors.New("file not found")}
}

func cleanup() {
	if r := recover(); r != nil {
		fmt.Println("Recovered from panic:", r)
	}
}

type TimeoutError struct {
	Operation string
	Timeout   time.Time
}

func (e *TimeoutError) Error() string {
	return fmt.Sprintf("Timeout error during %s operation. Timeout at %s", e.Operation, e.Timeout.Format("2006-01-02 15:04:05"))
}

// error 为接口
func PerformOperation() error {
	timeout := time.Now().Add(5 * time.Second)
	if !time.Now().After(timeout) {
		return &TimeoutError{Operation: "PerformOperation", Timeout: timeout}
	}
	return nil
}
